package com.hmdp.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;

    @Resource
    private IUserService userService;

    @ApiOperation(value = "用户保存博客信息")
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog){
        return blogService.saveBlog(blog);
    }

    @ApiOperation(value = "根据博客id判断某篇博客是否点赞过")
    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        // 修改点赞数量
        return blogService.likeBlog(id);
    }

    @ApiOperation(value = "根据博客id查询某篇文章的top5点赞用户")/*这里是根据时间搓来判断的*/
    @GetMapping("/likes/{id}")
    public Result likesByBlogId(@PathVariable("id") Long id){
        return blogService.likesByBlogId(id);
    }

    @GetMapping("/hot")
    @ApiOperation(value = "首页查询热点记录")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }


    @ApiOperation(value = "根据blog的id获取博客基本信息")
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }

    @ApiOperation(value = "根据用户信息查询blog")
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current",defaultValue = "1") Integer current,
            @RequestParam("id") Long id
    ){
        /*根据用户查询*/
        Page<Blog> page = blogService.query().eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        /*获取当前页数据*/
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @ApiOperation(value = "滚动分页查询")
    @GetMapping("/of/follow")
    /*第一个参数是：上一次的最小时间搓，第二个参数是偏移量，offset是跳过的元素的个数*/
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max,@RequestParam(value = "offset",defaultValue = "0") Integer offset){
        return blogService.queryBlogOfFollow(max,offset);
    }

    @ApiOperation(value = "根据当前登录用户分页查询博客信息")
    @GetMapping("/of/me")
    public Result queryBlog(@RequestParam(value = "current",defaultValue = "1") Integer current){
        /*1.查询当前用户id*/
        Long userId = UserHolder.getUser().getId();
        if(userId==null){
            return Result.fail("请先登录~");
        }
        /*2.根据当前用户分页查找博客信息*/
        Page<Blog> blogPage = new Page<>(current, SystemConstants.MAX_PAGE_SIZE);
        Page<Blog> blogs = blogService.query().eq("user_id", userId).page(blogPage);
        List<Blog> records = blogs.getRecords();
        return Result.ok(records);
    }



}
